package com.astar.controller.systemSetup;


import com.astar.dto.result.DefaultResult;
import com.astar.entity.CusUser;
import com.astar.entity.Dept;
import com.astar.service.IDeptService;
import com.astar.service.IUserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-01-17
 */
@Controller
@RequestMapping("/userManage")
public class UserController {

    @Autowired
    private IUserService userServiceImpl;

    @Autowired
    private IDeptService deptServiceImpl;



    @RequestMapping("/index")
    public Object toIndex(){
        return "userManage/index";
    }
    @RequestMapping("/queryUserAll")
    @ResponseBody
    public Object queryUserAll(Integer deptId,String userName,String loginName){
        return userServiceImpl.queryUserByDeptId(deptId,userName,loginName);
    }


    @RequestMapping("/department")
    @ResponseBody
    public Object department(){
        List<Map<String,Object>> mapList = new ArrayList<>();
        List<Dept> deptList = deptServiceImpl.selectAll();
        for (Dept dept : deptList) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",dept.getDeptId());
            map.put("pId",dept.getPId());
            map.put("name",dept.getDeptName());
            mapList.add(map);
        }
        return mapList;
    }

    @RequestMapping("/toView")
    public Object toView(Integer userId, Model model){
        CusUser cusUser = userServiceImpl.selectForid(userId);
        model.addAttribute("cusUser",cusUser);
        return "userManage/view";
    }

    @RequestMapping("/toUpdate")
    public Object toUpdate(Integer userId, Model model){
        CusUser cusUser = userServiceImpl.selectForid(userId);
        model.addAttribute("cusUser",cusUser);
        return "userManage/update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(CusUser cusUser, Model model){
        SimpleHash simpleHash = new SimpleHash("MD5",cusUser.getPassword(),cusUser.getSalt(),16);
        cusUser.setPassword(simpleHash.toString());
        return DefaultResult.buildDataResult(userServiceImpl.updateForId(cusUser));
    }



    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(CusUser cusUser){
        return DefaultResult.buildDataResult(userServiceImpl.delete(cusUser));
    }

    @RequestMapping("/tree")
    public Object tree(){
        return  "userManage/tree";
    }






}
